package cn.appsys.controller;

import cn.appsys.pojo.*;
import cn.appsys.service.*;
import lombok.extern.log4j.Log4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Log4j
@Controller
@RequestMapping("/back")
public class BackendUserController {
    @Autowired
    private BackendUserService backendUserService;
    @Autowired
    private AppInfoService appinfoService;
    @Autowired
    private DataDictionaryService dataDictionaryService;
    @Autowired
    private AppCategoryService appCategoryService;
    @Autowired
    private AppversionService appversionService;
    @RequestMapping("/login")
    public String login(HttpServletRequest request, HttpSession session, @RequestParam(required = false) String userCode, @RequestParam(required = false) String userPassword){
        log.debug("用户登录");
        BackendUser user = this.backendUserService.getLoginUser(userCode);
        if (user != null) {
            if (user.getUserPassword().equals(userPassword)) {
                session.setAttribute("userSession", user);
                return "backend/main";
            } else {
                request.setAttribute("message","密码错误");
                return "backendlogin";
            }
        } else {
            request.setAttribute("message","账号不存在");
            return "backendlogin";
        }
    }
    @RequestMapping("/toLogin")
    public String login() {
        log.debug("跳转到后台登录页面");
        return "backendlogin";
    }

    @RequestMapping(value = "/viewAppInfoList")
    public String viewAppInfoList(Model model,HttpSession session,
                                  @RequestParam(required = false,defaultValue = "") String softwareName,
                                  @RequestParam(required = false,defaultValue = "0") Integer flatformId,
                                  @RequestParam(required = false,defaultValue = "0") Integer categoryLevel1,
                                  @RequestParam(required = false,defaultValue = "0") Integer categoryLevel2,
                                  @RequestParam(required = false,defaultValue = "0") Integer categoryLevel3,
                                  @RequestParam(required = false,defaultValue = "1") Integer currPageNo){

        if(softwareName!=null) {
            session.setAttribute("softwareName", softwareName);
        }
        if(flatformId!=null){
            session.setAttribute("flatformId",flatformId);
        }
            session.setAttribute("categoryLevel1",categoryLevel1);
            session.setAttribute("categoryLevel2",categoryLevel2);
            session.setAttribute("categoryLevel3",categoryLevel3);
        Page page = new Page();
        page.setPageSize(5);
        //当前页
        page.setCurrPageNo(currPageNo);
        page.setTotalCount(dataDictionaryService.getAppInfoListCout(softwareName, flatformId, categoryLevel1, categoryLevel2, categoryLevel3));
        if(page.getCurrPageNo()<=0){
            page.setCurrPageNo(1);
        }
        model.addAttribute("currPageNo",currPageNo);
        page.setList(dataDictionaryService.getAppInfoList(softwareName, flatformId, categoryLevel1, categoryLevel2, categoryLevel3,page));
        //分类
        List<DataDictionary> statuslist=dataDictionaryService.getStatus();
        model.addAttribute("status",statuslist);
        model.addAttribute("appInfoList",page.getList());
        model.addAttribute("page",page);
        return "backend/applist";
    }
    @RequestMapping("categoryLevel1")
    public String categoryLevel1(Model model){
        List<AppCategory> ca1=appCategoryService.getCategoryLevel1();
        model.addAttribute("listcategoryLevel1",ca1);
        return "fenlei/yiji";
    }
    @RequestMapping("categoryLevel2")
    public String categoryLevel2(Model model,@RequestParam Integer parentId2){
        List<AppCategory> ca2=appCategoryService.getCategoryLevel2(parentId2);
        model.addAttribute("listcategoryLevel2",ca2);
        return "fenlei/erji";
    }
    @RequestMapping("categoryLevel3")
    public String categoryLevel3(Model model,@RequestParam Integer parentId3){
        List<AppCategory> ca3=appCategoryService.getCategoryLevel3(parentId3);
        model.addAttribute("listcategoryLevel3",ca3);
        return "fenlei/sanji";
    }
    @RequestMapping("pingtaiName")
    public String pingtaiName(Model model,HttpSession session){
        //平台属性
        System.out.println(session.getAttribute("flatformId"));
        List<DataDictionary> terraceProperty = dataDictionaryService.getPintai();
        model.addAttribute("terraceProperty",terraceProperty);
        return "fenlei/pingtai";
    }
    @RequestMapping("appshen")
    public String appshen(Model model,@RequestParam Integer appid,@RequestParam Integer vid,@RequestParam String statusname){
        AppInfo appInfo=appinfoService.getAppInfoView(appid);
        Appversion appversion=appversionService.getbanbenId(vid);
        model.addAttribute("appInfo",appInfo);
        model.addAttribute("appVersion",appversion);
        return "backend/appshenhe";
    }
    @RequestMapping("appshenok")
    public String appshenok(@RequestParam("id") Integer id,@RequestParam("status") Integer status){
        int data=appinfoService.checkApp(status,id);
        if(data>0){
            return "redirect:/back/viewAppInfoList";
        }
        return "backend/appshenhe";
    }

}